<?php 

class AnswerControl extends CommonControl{
	public function index(){
		$w=isset($_GET['w'])?(int)$_GET['w']:1;
		switch ($w) {
			case 1:
				$where=M('answer');
				break;
			case 2:
				$where=M('answer')->where('accept=0');
				break;
			case 3:
				$where=M('answer')->where('accept=1');
				break;	
			default:
				$where=M('answer');		
		}
		$data=$where->all();
		if(!is_null($data))$this->assign('answer',$data);
		
		$this->display();
	}
		public function del_answer(){
				if(isset($_GET['anid'])){
					$da=M('answer')->where('anid='.(int)$_GET['anid'])->find();
					if(isset($da)){

				if($da['accept']==0){
					$data=M('answer')->where('anid='.(int)$_GET['anid'])->delete();
					if(isset($data))$this->error('删除成功');
					$this->error('删除失败');
				}
				if($da['accept']==1){
					$user=M('user')->field('point,uid')->where('uid='.$da['uid'])->find();
					$field='point,u.uid,reward';
					$info1=M('ask')->query("SELECT {$field} from hd_ask as ask join hd_user as u on ask.uid=u.uid where asid={$da['asid']}");
					// 删除问题
					$data=M('answer')->where('anid='.(int)$_GET['anid'])->delete();
					M('ask')->where('asid='.$da['asid'])->update(array('solve'=>0));
					if(isset($info1[0])&&isset($user['point'])){
						if($user['point']>$info1[0]['reward']){
							$reward=$info1[0]['reward'];
							$point=M('ask')->exec("update hd_user set point=point-{$reward} where  uid={$user['uid']} ");
							$answer=M('ask')->exec("update hd_user set answer=answer-1 where  uid={$user['uid']} ");
							
							
						}
					}
					if(isset($point)&&isset($data)&&isset($answer))$this->error('删除成功且扣除金币');
					if(isset($data)||isset($point))$this->error('删除成功');
					}
					
				}
		}


	}
}
 ?>